package com.example.demo.controller;

import com.example.demo.Util.BaseUtil;
import com.example.demo.entity.*;
import com.example.demo.mapper.MonitorcodeInfoMapper;
import com.example.demo.mapper.MonitorobjInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tanjun on 2021-12-05.
 */
@Controller
public class AccessTestConfigController {

    @Autowired
    MonitorcodeInfoMapper monitorcodeInfoMapper;

    @RequestMapping("/AccessTestConfig")
    public String index(Model model,HttpServletRequest request){
        String userName= BaseUtil.getCurrentLoginUserName();
        model.addAttribute("userName", userName);
        return "accessTestConfig";
    }

    @RequestMapping("/getMonitorCodeList")
    @ResponseBody
    public Map getMonitorCodeList(HttpServletRequest request){
        MonitorcodePageInfo PageInfo=new MonitorcodePageInfo();
        PageInfo.setPageNumber(Integer.valueOf(request.getParameter("page")));
        PageInfo.setPageSize(Integer.valueOf(request.getParameter("rows")));
        List<MonitorcodeInfo> lists=monitorcodeInfoMapper.queryMonitorCodeInfos(PageInfo);
        Map m=new HashMap();
        m.put("total", monitorcodeInfoMapper.queryMonitorCodeCounts());
        m.put("rows", lists);
        return m;
    }

    @RequestMapping("/saveMonitorCode")
    @ResponseBody
    public ResultVo saveMonitorObj( MonitorcodeInfo monitorcodeInfo){
        String vaildResult=monitorcodeInfo.vaildSelfRequriedAtrribute();
        if(!"success".equals(vaildResult)){
            throw new RuntimeException(vaildResult);
        }
        if(monitorcodeInfo.getMonitorcodeId()==null||"".equals(monitorcodeInfo.getMonitorcodeId())){
            //如果没有ID，则是新增
            monitorcodeInfo.setMonitorcodeId(BaseUtil.getUUID());
            monitorcodeInfo.setUpdatetime(new Date());
            monitorcodeInfo.setEffective("1");
            monitorcodeInfoMapper.insert(monitorcodeInfo);
        }else {
            //有ID则是，更新
            monitorcodeInfoMapper.updateByPrimaryKeySelective(monitorcodeInfo);
        }
        return new ResultVo(true,null);
    }

    @RequestMapping("/updateMonitorCodeInfo")
    @ResponseBody
    public void updateMonitorCodeEffective(MonitorcodeInfo monitorcodeInfo) {
        monitorcodeInfoMapper.updateByPrimaryKeySelective(monitorcodeInfo);
    }

    @RequestMapping("/removeMonitorCode")
    @ResponseBody
    public void removeMonitorObj(String monitorcodeId) {
        monitorcodeInfoMapper.deleteByPrimaryKey(monitorcodeId);
    }

    @RequestMapping("initMonitorobjCodeIdSele")
    @ResponseBody
    public List<Map> initMonitorobjCodeIdSele(){
        List<Map> list = monitorcodeInfoMapper.queryMonitorCodeSele();
        return list;
    }
}
